local function AT2_IsSecure()
	AT2_Print(debugstack(2, 1, 1),"debug","AT2_IsSecure")
	if (debugstack(2,1,1):find("Interface\\AddOns\\AutoTalker2\\")) then
		return true
	else
		return false
	end
end


function AT2_Print(msg, type, func)
	msg=AT2_StringFormat(msg)
	local n = "\124cff71D5FF\A\124r\124cff0070DD\uto\124r\124cff71D5FF\T\124r\124cff0070DDalker\124r\124cffFFFF332\124r"
	if (type == "debug") then
		if (AT2_Save and AT2_Save["debugenabled"]) then
			DEFAULT_CHAT_FRAME:AddMessage("["..n.."\124r] \124cffFF8000Debug:\124r \124cff71D5FF"..func..",\124r "..msg,1,1,0)
		end
	elseif (type == "error") then
		DEFAULT_CHAT_FRAME:AddMessage("["..n.."\124r] \124cffFF0000Hiba!\124r "..msg,1,1,0)
	else
		DEFAULT_CHAT_FRAME:AddMessage("["..n.."\124r] "..msg,1,1,0)
	end
end

function AT2_Lang_Plural(num,singular,plural,between)
	local between=between or ""
	if (num==1) then
		return num..between.." "..singular
	else
		return num..between.." "..plural
	end
end

function AT2_StringFormat(msg)
msg=msg:gsub("AT2CW","\124cffFFFFFF"):gsub("AT2CR","\124cffFF0000"):gsub("AT2CG","\124cff00FF00"):gsub("AT2CO","\124cffFF8000"):gsub("AT2CE","\124r") -- "AutoTalker2 Color (White | Red | Green | Orange | End)"
msg=msg:gsub("•yellow","\124cffFFFF00"):gsub("•red","\124cffFF0000"):gsub("•cyan","\124cff00FFFF"):gsub("•gold","\124cffF0CC00"):gsub("•green","\124cff00FF00"):gsub("•gray","\124cff9d9d9d"):gsub("•xc","\124r") -- new style coloring, previous kept for compatibility until removing referenes
return msg
end


function AT2_Slash(slashcmd,...)
	if slashcmd == "" then AT2_UI:Show() end
	AT2_Print(slashcmd, "debug","Slash")
	if slashcmd == "debug" then
		if not(AT2_Save["debugenabled"]) then 
			AT2_Save["debugenabled"] = true
			AT2_Print("AT2_Save[\"debugenabled\"] = true","debug","AT2_Slash/debug")
		else
			AT2_Save["debugenabled"] = false
			AT2_Print("Debug mód kikapcsolva")
		end
	elseif slashcmd=="debug clear savedmessages" then
		AT2_Save["Messages"]=nil
		AT2_Print("Mentett üzenetek törölve.")
	elseif slashcmd=="debug clear all" then
		AT2_Save=nil
		AT2_Print("Minden mentett adat törölve. A /reloadui parancs lefuttatása ajánlott a hibák elkerülése végett.")
	end	
end

SlashCmdList["AT2"] = AT2_Slash;
SLASH_AT21 = "/at"
SLASH_AT22 = "/autotalker"
SLASH_AT23 = "/at2"
SLASH_AT24 = "/autotalker2"

function tcount(tab)
   local n = 0
   for _ in pairs(tab) do
     n = n + 1
   end
   return n
end

function AT2_TotalTime(sec)
	if (sec < 60 and sec > 9) then return "0:"..sec
	elseif (sec < 10) then return "0:0"..sec
	else
		local secminusmin = sec-(floor(sec/60)*60)
		local returntime
		if (secminusmin < 10) then 
			returntime= floor((sec)/60)..":0"..secminusmin
		else
			returntime= floor((sec)/60)..":"..secminusmin
		end
    return returntime
	end
end

function isBetween(what,bottom,top)
	return (what >= bottom and what <= top)
end

function GetAbsolutePosition(frame)
local offsetX=0
local offsetY=0
local screenX,screenY=strsplit("x",select(GetCurrentResolution(),GetScreenResolutions()))
local frameX=select(4,frame:GetPoint())
local frameY=select(5,frame:GetPoint())
if select(1,frame:GetPoint())=="TOPLEFT" then 			offsetX=frameX;						 offsetY=((screenY/3)*2)+frameY
elseif select(1,frame:GetPoint())=="TOP" then 			offsetX=(screenX/3)+frameX;			 offsetY=((screenY/3)*2)+frameY
elseif select(1,frame:GetPoint())=="TOPRIGHT" then 		offsetX=((screenX/3)*2)+frameX;		 offsetY=((screenY/3)*2)+frameY
elseif select(1,frame:GetPoint())=="LEFT" then 			offsetX=frameX;						 offsetY=(screenY/3)+frameY;
elseif select(1,frame:GetPoint())=="CENTER" then 		offsetX=(screenX/3)+frameX; 		 offsetY=(screenY/3)+frameY;
elseif select(1,frame:GetPoint())=="RIGHT" then 		offsetX=((screenX/3)*2)+frameX;		 offsetY=(screenY/3)+frameY;
elseif select(1,frame:GetPoint())=="BOTTOMLEFT" then 	offsetX=frameX;					  	 offsetY=frameY;
elseif select(1,frame:GetPoint())=="BOTTOM" then 		offsetX=(screenX/3)+frameX;			 offsetY=frameY;
elseif select(1,frame:GetPoint())=="BOTTOMRIGHT" then 	offsetX=((screenX/3)*2)+frameX;		 offsetY=frameY;
end
AT2_UI_Title:SetText("\124cff71D5FF\A\124r\124cff0070DD\uto\124r\124cff71D5FF\T\124r\124cff0070DDalker\124r\124cffFFFF33 2.0\124r (alpha) "..math.floor(offsetX).." "..math.floor(offsetY).." "..math.floor(GetCursorPosition()).." "..math.floor(select(2, GetCursorPosition())))
return offsetX,offsetY
end

function tremovebyval(tab, val)
   for k,v in pairs(tab) do
     if(v==val) then
       table.remove(tab, k);
       return true;
     end
   end
   return false;
 end

if not(AT2_ModuleList) then
	AT2_ModuleList = {}
end
tinsert(AT2_ModuleList,"Misc");